//
//  SHHttpManager.m
//  ShinyOrder
//
//  Created by 王方伟 on 16/7/25.
//  Copyright © 2016年 ShinyHub. All rights reserved.
//

#import "SHHttpUtil.h"

@interface SHHttpUtil ()
@property (strong, nonatomic) AFHTTPSessionManager *manager;
@end

@implementation SHHttpUtil
    
+ (instancetype)sharedHttpUtil{
    static SHHttpUtil *httpUtil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        httpUtil = [[SHHttpUtil alloc] init];
    });
    return httpUtil;
}

- (AFHTTPSessionManager *)manager{
    if (_manager == nil) {
        _manager = [[AFHTTPSessionManager alloc] init];
        _manager.requestSerializer=[AFJSONRequestSerializer serializer];
        _manager.operationQueue.maxConcurrentOperationCount = 3;
        [_manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
        [_manager.requestSerializer setTimeoutInterval:10];
        [_manager.requestSerializer setStringEncoding:NSUTF8StringEncoding];
        _manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil];
    }
    return _manager;
}

+(void)cancelManager{
    [[SHHttpUtil sharedHttpUtil].manager.tasks makeObjectsPerformSelector:@selector(cancel)];
}



#pragma mark 发送get请求
+ (NSURLSessionDataTask *)getWithURL:(NSString *)url params:(NSDictionary *)params success:(void (^)(id))success failure:(void (^)(NSError *))failure{
    NSURLSessionDataTask *task = [[SHHttpUtil sharedHttpUtil].manager GET:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            [self showErrorInfo:error];
            failure(error);
        }
    }];
    return task;
}

#pragma mark 发送Delete 请求
+ (NSURLSessionDataTask *)deleteWithURL:(NSString *)url params:(NSDictionary *)params success:(void (^)(id))success failure:(void (^)(NSError *))failure{
    NSURLSessionDataTask *task = [[SHHttpUtil sharedHttpUtil].manager DELETE:url parameters:params success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            [self showErrorInfo:error];
            failure(error);
        }
    }];
    return task;
}

#pragma mark 发送put请求
+ (NSURLSessionDataTask *)putWithURL:(NSString *)url params:(NSDictionary *)params success:(void (^)(id))success failure:(void (^)(NSError *))failure{
    NSURLSessionDataTask *task = [[SHHttpUtil sharedHttpUtil].manager PUT:url parameters:params success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            [self showErrorInfo:error];
            failure(error);
        }
    }];
    return task;
}

#pragma mark 发送post请求
+ (NSURLSessionDataTask *)postWithURL:(NSString *)url params:(NSDictionary *)params success:(void (^)(id))success failure:(void (^)(NSError *))failure{
    NSURLSessionDataTask *task = [[SHHttpUtil sharedHttpUtil].manager POST:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            [self showErrorInfo:error];
            failure(error);
        }
    }];
    return task;
}

+(NSURLSessionDataTask *)postWithURL:(NSString *)url params:(NSDictionary *)params formDataArray:(NSArray *)formDataArray success:(void (^)(id))success failure:(void (^)(NSError *))failure{
    NSURLSessionDataTask *task = [[SHHttpUtil sharedHttpUtil].manager POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        for (FormData *data in formDataArray) {
            [formData appendPartWithFileData:data.data name:data.name fileName:data.filename mimeType:data.mimeType];
        }
 } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
     if (success) {
         success(responseObject);
     }
 } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
     if (failure) {
         [self showErrorInfo:error];
         failure(error);
     }
 }];
    return task;
}


#pragma mark 显示错误信息
+ (void)showErrorInfo:(NSError * _Nonnull) error{
    int errorCode = [error.userInfo[@"_kCFStreamErrorCodeKey"] intValue];
    switch (errorCode) {
        case -2102://请求超时
        
            break;
            
        case 8://似乎已断开与互联网的连接
        
            break;
            
        case 50://网络连接已中断
        
            break;
            
        case 57://网络连接已中断
        
            break;
            
        case 61://未能连接到服务器
        
            break;
        default:
        
            break;
    }
}
@end

@implementation FormData
@end
